/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <rom.prevot@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return R. Prevot
 * ----------------------------------------------------------------------------
 */
/**
 * ****************************************************************************\
 * ____ _______ _____ _ _ * | _ \ |__ __| / ____| | | | * | |_) | ___ _ __ _ __
 * | | ___| (___ | |_ ___ ___| | __ * | _ < / _ \| '__| '_ \| |/ _ \\___ \| __/
 * _ \ / __| |/ / * | |_) | (_) | | | | | | | (_) |___) | || (_) | (__| < *
 * |____/ \___/|_| |_| |_|_|\___/_____/ \__\___/ \___|_|\_\ * *
\*****************************************************************************
 */
package app.models;

import app.Tools;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

/**
 *
 * @author Ghost
 */
public class Factory {

    /**
     * "usine" d'objet de gestion de contexte
     */
    private static EntityManagerFactory contextFactory;
    /**
     * Contexte de persistence
     */
    private static EntityManager context = null;
    /**
     *
     */
    private static EntityTransaction tx;
    /**
     * Setup var to know if we need to install tables
     */
    private static boolean setup = true;

    /**
     * Permet d'initialisé le context de persistence et de l'utiliser
     *
     * @return context
     */
    public static EntityManager getContext() {
        if (context == null) {
            Map prop = new HashMap();
            if (setup) {
                prop.put("eclipselink.ddl-generation", "create-tables");
            } else {
                prop.put("eclipselink.ddl-generation", "none");
            }
            prop.put("eclipselink.logging.exceptions", "false");

            contextFactory = Persistence.createEntityManagerFactory("BornToStockPU", prop);
            context = contextFactory.createEntityManager();
        }
        if (tx == null) {
            tx = context.getTransaction();
        }
        if (!tx.isActive()) {
            tx.begin();
        }

        return context;
    }

    /**
     * Met un objet dans le context de persistence @todo assert context != null
     *
     * @param o
     */
    public static void persist(Object o) {
        try {
            context = getContext();
            context.persist(o);
        } catch (Exception e) {
            System.err.println(e);
        }
    }

    /**
     *
     * @todo assert context != null
     *
     * @param o
     */
    public static void save(Object o) {
        try {
            tx = context.getTransaction();
            if (!tx.isActive()) {
                tx.begin();
            }

            context = getContext();
            if (!context.contains(o)) {
                context.persist(o);
            }
            context.merge(o);

            tx.commit();
        } catch (Exception e) {
            Tools.debug("3:" + e);
        }
    }

    /**
     * @todo assert context != null
     *
     * @param o
     */
    public static void remove(Object o) {
        tx = context.getTransaction();
        if (!tx.isActive()) {
            tx.begin();
        }
        getContext().remove(o);
        tx.commit();
    }
}
